package com.jwater.console.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.jwater.console.model.Result;

/**
 * @description: 拦截所有的内部报错，返回通用的错误信息
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handlerSpaceMoneyException(Throwable e) {
        if (e instanceof NotConnectException) {
            return Result.error("请先启动管理进程");
        }
        logger.error(e.toString(), e);
        return Result.error(e.getMessage());
    }
}
